<template>
  <div class="searh-container">
    <!-- 顶部的搜索栏开始 -->
    <!-- Tips:在 van-search 外层增加 form 标签，且 action 不为空，即可在 iOS
    输入法中显示搜索按钮。 -->
    <form class="search-form" action="/">
      <van-search
        v-model="searchText"
        show-action
        placeholder="请输入搜索关键词"
        background="#3296fa"
        @search="onSearch"
        @cancel="onCancel"
        @focus="isResultShow = false"
      />
    </form>
    <!-- 顶部的搜索栏结束 -->

    <!-- 搜索结果开始 -->
    <search-result v-if="isResultShow" :search-text="searchText" />

    <!-- 搜索结果结束 -->
    <!-- 联想建议开始 -->
    <search-suggestion
      v-else-if="searchText"
      :search-text="searchText"
      @search="onSearch"
    />

    <!-- 联想建议结束 -->
    <!-- 搜索历史记录开始 -->
    <search-history
      v-else
      :search-histories="searchHistories"
      @clear-search-histories="searchHistories = []"
      @search="onSearch"
    />
    <!-- 搜索历史记录结束 -->
  </div>
</template>
<script>
import SearchHistory from './components/search-hsitory'
import SearchSuggestion from './components/search-suggestion'
import SearchResult from './components/search-result'
import { setItem, getItem } from '@/utils/storage'
export default {
  name: 'SearchIndex',
  components: { SearchHistory, SearchSuggestion, SearchResult },
  props: {},
  data() {
    return {
      searchText: '',
      isResultShow: false, // 控制搜索结果的展示
      searchHistories: getItem('TOUTIAO_SEARCH_HISTORIES') || [] // 搜索历史记录数据
    }
  },
  computed: {},
  watch: {
    searchHistories(value) {
      console.log('haha')
      setItem('TOUTIAO_SEARCH_HISTORIES', value)
    }
  },
  methods: {
    onSearch(val) {
      // 更新文本框内容
      this.searchText = val

      // 储存搜索历史记录
      // 要求 不要有重复历史记录，最新的排在最前面

      const index = this.searchHistories.indexOf(val)
      if (index !== -1) {
        // 移除重复的历史记录
        this.searchHistories.splice(index, 1)
      }
      this.searchHistories.unshift(val)

      // 渲染搜索结果
      this.isResultShow = true
    },
    onCancel() {
      this.$router.back()
    }
  },
  created() {},
  mounted() {}
}
</script>
<style scoped lang="less">
.searh-container {
  padding-top: 108px;
  .van-search__action {
    color: #ffffff;
    font-size: 28px;
  }
  .search-form {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1;
  }
}
</style>
